package com.example.lenovo.a203_yhy_ordering_3.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.lenovo.a203_yhy_ordering_3.R;
import com.example.lenovo.a203_yhy_ordering_3.beans.LoginBean;
import com.example.lenovo.a203_yhy_ordering_3.listener.LoginListener;
import com.example.lenovo.a203_yhy_ordering_3.model.LoginModel;

public class LoginActivity extends AppCompatActivity {

    private EditText username;//账号
    private EditText userpass;//密码
    private Button btn_login;//登录
    private Button btn_reg;//注册
    private SharedPreferences sharedPreferences,sharedPreferences1;

    private LoginListener loginlistener = new LoginListener() {
        @Override
        public void onResponse(LoginBean login) {
            if(login.getUserid().equals("0")){
                Toast.makeText(LoginActivity.this,"用户名或密码错误",Toast.LENGTH_SHORT).show();
            }
            else if(username.getText().toString().equals("")||userpass.getText().toString().equals("")){
                Toast.makeText(LoginActivity.this,"用户名不能为空",Toast.LENGTH_SHORT).show();
            }
            else{
                Toast.makeText(LoginActivity.this,"登陆成功",Toast.LENGTH_SHORT).show();
                sharedPreferences.edit()
                        .putString("userid",login.getUserid())
                        .apply();
                sharedPreferences1.edit()
                        .putString("userpass", userpass.getText().toString())
                        .apply();
                Intent intent = new Intent(LoginActivity.this,MainActivity.class);
                startActivity(intent);
                finish();
            }
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(LoginActivity.this,"登录失败",Toast.LENGTH_SHORT).show();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        sharedPreferences=getSharedPreferences("user",MODE_PRIVATE);
        sharedPreferences1=getSharedPreferences("userpass",MODE_PRIVATE);

        initViews();
        initEvents();
    }

    private void initViews(){
        btn_reg=(Button) findViewById(R.id.button2);
        btn_login=(Button)findViewById(R.id.button);
        username=(EditText)findViewById(R.id.editText);
        userpass=(EditText)findViewById(R.id.editText2);
    }

    private void initEvents(){
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LoginModel loginModel = new LoginModel();
                loginModel.getResultList(
                        username.getText().toString(),
                        userpass.getText().toString(),
                        loginlistener);
            }
        });
        btn_reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent =new Intent(LoginActivity.this,RegisterActivity.class);
                startActivity(intent);
            }
        });
    }







}

